The example below\footnote{in tests\textbackslash exceptionEx.tool}, composed of 4 parts, shows how to create and use exceptions with the extended \textsc{Tool} language.
In the first part (see listing \ref{ExceptionDefinition}), three new classes of exception are defined.
The second part defines an abstract data structure for array of positive integers (see listing \ref{ArrayDefinition}).
The third one shows 3 use cases for those exceptions (see listing \ref{SenarioUsage}). And finally, the last one is the main program (see listing \ref{MainProgram}) which shows nested \emph{try-catch} structure.

\setToolListing

\begin{lstlisting}[language=tool,caption=Exceptions definition,label=ExceptionDefinition]
class UninitializedException extends ToolException { }
class NegativeValueException extends ToolException { }

class OutOfBoundException extends ToolException {
  var maxIndex : Int;
  
  def setMaxBound(max : Int) : Int = {
    maxIndex = max-1;
    return max;
  }
  
  def setAccessingIndex(i : Int) : OutOfBoundException = {
    msg = "Index " + i + " is out of bound";
    
    return this;
  }
  
  def getMessage() : String = {
    return msg + " (max index " + maxIndex + ")";
  }
}
\end{lstlisting}

The first two new exceptions do not add any extra functionalities to the base exception.
They just define more specific exception names to be used in special error cases.
However, in order to output more precise error messages, the \textsf{\small OutOfBoundException} class defines 2 additional methods and overrides the \textsf{\small getMessage()} method inherited from \textsf{\small ToolException}.

\begin{lstlisting}[language=tool,caption=Positive array definition,label=ArrayDefinition]
class PositiveArray {
  var outOfBoundException : OutOfBoundException;
  var array : Int[];
  var size : Int;
  var nonInitValue : Int;
  
  /* Create an non-initialized array of size s*/
  def init(s : Int) : Int = {
    var i : Int;
    
    size = s;
    array = new Int[size];
    nonInitValue = 0-1;
    
    i = 0;
    while (i < size) {
      array[i] = nonInitValue;
      i = i+1;
    }
    
    outOfBoundException = new OutOfBoundException();
    return outOfBoundException.setMaxBound(size);
  }
  
  /* Set at index i the given value */
  def set(i : Int,value : Int) : Int throws ToolException = {
    if (i < size) {
      if (value < 0)
        throw new NegativeValueException().
          setMessage("Value must be strictly positive");
      
      array[i] = value;
    } else {
      throw outOfBoundException.setAccessingIndex(i);
    }
    
    return array[i];
  }
  
  /* Return the value at the given index i */
  def get(i : Int) : Int
    throws OutOfBoundException, UninitializedException = {

    if (i < size) {
      if (array[i] < 0)
        throw new UninitializedException().
          setMessage("Value at index " + 
                     i + " was not initialized");
    } else {
      throw outOfBoundException.setAccessingIndex(i);
    }
    
    return array[i];
  }
}
\end{lstlisting}

Since the argument of a \textsf{\small throw} statement is an expression, exceptions can be created on-the-fly, just prior to be raised (\textit{e.g.} as in the \textsf{\small get} method).
They can also be created, initialized and raised later on (\textit{e.g.} as in the second \textsf{\small throw} statement in the \textsf{\small get} method).

As you can see, whenever an expression is raised but not caught within a method, that method needs to declare it with its \textsf{\small throws} statement. The precise exception type can be used, as in the \textsf{\small get} method or a supertype of all the thrown exception can be used, as in the \textsf{\small set} method (this is however considered as a bad practice).
\begin{lstlisting}[language=tool,caption=Use case,label=SenarioUsage]
class ArrayUse {
  var array : PositiveArray;
  var bin : Int; // Never used
  
  def useUninitializedArray() : Int throws ToolException = {
    array = new PositiveArray();
    bin = array.init(5);
    
    // Raise an UninitializedException
    return array.get(2);
  }
  
  def useOutOfBoundArray() : Int throws OutOfBoundException, NegativeValueException = {
    array = new PositiveArray();
    bin = array.init(5);
    
    // Raise an OutOfBoundException
    return array.set(5,5);
  }
  
  def useNegativeValueArray() : Int throws NegativeValueException, OutOfBoundException = {
    array = new PositiveArray();
    bin = array.init(5);
    
    // Raise an NegativeValueException
    return array.set(0,0-2);
  }
}
\end{lstlisting}

The list of all the exceptions declared in the \textsf{\small throws} statements of methods are contained in the method symbols.
When a method is called, the analyzer checks that all the exceptions contained in that method symbol are handled, \textit{i.e.} either caught or thrown upward. If it is not the case, the code won't compile.

\begin{lstlisting}[language=tool,caption=Main program,label=MainProgram]
object ExceptionEx {
  def main() : Unit = {
    try {
      try {
        println(new ArrayUse().useUninitializedArray());
      } catch (e : UninitializedException) {
        println("ERROR " + e.getMessage());
      }
      
      try {
        println(new ArrayUse().useOutOfBoundArray());
      } finally {
        println("spongebob");
      }
    } catch (e : OutOfBoundException) {
      println("ERROR " + e.getMessage());
      
      try {
        println(new ArrayUse().useNegativeValueArray());
      } catch (e2 : ToolException) {
        println("ERROR " + e2.getMessage());
      }
    } finally {
        println("END OF EXCEPTIONS DEMO");
    }
  }
}
\end{lstlisting}

This very messy \textsf{\small main} shows how exceptions are caught (or not!). 
Note that \textsf{\small try-catch-finally} blocks can be nested (that should be avoided though).
Exceptions will be caught in the innermost \textsf{\small catch} block whenever possible.
Also, note that exceptions will be caught in a \textsf{\small catch} statement containing any of its superclass.
The semantic of TOOL's \textsf{\small finally} is equivalent to Java's : it is always executed regardless of the execution path within the corresponding \textsf{\small try-catch} blocks.
However, if a \textsf{\small finally} block is executed after an exception has been raised, the exception will still be raised once the \textsf{\small finally} block has been executed (\textit{e.g.} spongebob message).

\begin{lstlisting}[language=tool,caption=Output program,label=Output]
ERROR Value at index 2 was not initialized
spongebob
ERROR Index 5 is out of bound (max index 4)
ERROR Value must be strictly positive
END OF EXCEPTIONS DEMO
\end{lstlisting}
